package com.doradance.app.mine;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.doradance.R;
import com.doradance.account.UrlCenter;
import com.doradance.account.utils.ShareUtil;
import com.doradance.account.utils.XCRoundRectImageView;
import com.doradance.app.home.CoursesDetailActivity;
import com.doradance.app.mine.adapter.MineCollectAdapter;
import com.doradance.common.http.MyHttpRequest;
import com.doradance.entity.FreeItemEntity;
import com.doradance.entity.StartVideoEntity;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeListFragment;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.framework.httprequest.responsecallbacks.ListDataRequestListener;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class StartVideoFragment extends SwipeListFragment {

    private int mPage = 0;
    public StartVideoFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        startLoading();
        clearDecorations();
        return view;
    }

    @Override
    public void onStartRefreshing() {
        super.onStartRefreshing();
        getData().clear();
        mPage = 0;
        startLoading();
    }


    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long itemId) {
        super.onItemClick(arg0, arg1, position, itemId);
        StartVideoEntity o = (StartVideoEntity) getData().get(position);
        CoursesDetailActivity.start(getActivity(),o.getId());
    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new StartVideoAdapter(getContext());
    }
    private ListDataRequestListener<StartVideoEntity> mResponceListener = new ListDataRequestListener<>(this, StartVideoEntity.class);
    @Override
    public void onStartLoading() {
        if (mPage == 0 || mResponceListener.getResponseDataParser().hasMore()) {
            mPage++;
        }
            MyHttpRequest request = new MyHttpRequest(getContext());
            UrlParams urlParams = new UrlParams();
            String format = String.format(UrlCenter.START_VIDEO, ShareUtil.getBabyId());
            request.get(format, urlParams, mResponceListener);
    }


    class StartVideoAdapter extends RecyclerViewBaseAdapter<StartVideoEntity> {
        public StartVideoAdapter(Context con) {
            super(con);
        }

        @Override
        public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
            return new VH(setAdapterViewItem(parent, R.layout.start_video_item));
        }

        @Override
        public void onBindHolder(RecyclerView.ViewHolder viewHolder, int position) {
            VH holder = (VH) viewHolder;
            final StartVideoEntity startVideoEntity = getData().get(position);
            holder.mBtnStart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CoursesDetailActivity.start(getActivity(),startVideoEntity.getId());
                }
            });
            if(!TextUtils.isEmpty(startVideoEntity.getProgress_text())){
                holder.mTvStartTitle.setVisibility(View.VISIBLE);
                holder.mTvStartTitle.setText(startVideoEntity.getLast_chapter_name()+"("+startVideoEntity.getProgress_text()+")");
            }else{
                holder.mTvStartTitle.setVisibility(View.GONE);
            }
            holder.mTvStartContent.setText("【"+startVideoEntity.getCategory()+"】"+startVideoEntity.getName());
            Glide.with(getContext()).load(startVideoEntity.getCover()).asBitmap().into(holder.mXvStartVideo);
        }

        class VH extends RecyclerView.ViewHolder {
            @Bind(R.id.xv_start_video)
            XCRoundRectImageView mXvStartVideo;
            @Bind(R.id.tv_start_title)
            TextView mTvStartTitle;
            @Bind(R.id.rl_start_video)
            RelativeLayout mRlStartVideo;
            @Bind(R.id.tv_start_content)
            TextView mTvStartContent;
            @Bind(R.id.btn_start)
            Button mBtnStart;
            public VH(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }

}
